
#include "../inc/Ui/TimViTriForm.h"
#include "../inc/Utility/MyParserXml.h"
#include "../inc/Ui/WebForm.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Web::Controls;
using namespace Osp::System;
using namespace Osp::Graphics;

int TimViTriForm::FormId = -1;

TimViTriForm::TimViTriForm(void) {
}

TimViTriForm::~TimViTriForm(void) {
}

bool TimViTriForm::Initialize() {
	Form::Construct(L"TimViTriForm");

	return true;
}

result TimViTriForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_XEMMAP);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	r = CreateWebControl();
	if (IsFailed(r)) {
		AppLog("CreateWebForm() has failed.\n");
		goto CATCH;
	}
	pWebTimViTri->LoadUrl("file:///Res/TimViTri.html");
	return r;
	CATCH: return false;
}

result TimViTriForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}
void TimViTriForm::SetParentForm(Form *pParent) {
	this->pFormParent = pParent;
}
void TimViTriForm::SetFrame(Frame *pFrame) {
	this->pFrame = pFrame;
}

void TimViTriForm::OnActionPerformed(const Osp::Ui::Control&source,
		int actionId) {

	switch (actionId) {
	case ID_SOFTKEY_XEMMAP: {

		String *latLng = pWebTimViTri->EvaluateJavascriptN("getLatLng();");
		if (latLng->GetLength() > 0) {

			HienThiCacViTriDangTimTrenMap(latLng);
			pFrame->SetCurrentForm(*pFormParent);
			pFormParent->RequestRedraw(true);
		}

		else {
			MessageBox msg;
			msg.Construct("Chào bạn", "Bạn chưa nhập tiêu đề", MSGBOX_STYLE_OK);
			int i = 0;
			msg.ShowAndWait(i);
			return;
		}
		AppLog("Vao Xem Map");
	}
		break;
	case ID_SOFTKEY_BACK:
		pFrame->SetCurrentForm(*pFormParent);
		pFormParent->RequestRedraw(true);
		AppLog("Vao back");
		break;
	}
}
result TimViTriForm::CreateWebControl() {
	result r = E_SUCCESS;
	int screen_width = 0;
	int screen_height = 0;

	/*screen*/
	r = SystemInfo::GetValue("ScreenWidth", screen_width);
	TryCatch(r == E_SUCCESS, , "SystemInfo: To get a value is failed");

	r = SystemInfo::GetValue("ScreenHeight", screen_height);
	TryCatch(r == E_SUCCESS, , "SystemInfo: To get a value is failed");

	/*Web*/
	pWebTimViTri = new Web();
	r = pWebTimViTri->Construct(Rectangle(0, 0, screen_width, screen_height
			- 38));
	TryCatch(r==E_SUCCESS,,"Web is not constructed\n");

	r = this->AddControl(*pWebTimViTri);
	TryCatch(r=E_SUCCESS, , "Web is not constructed");
	pWebTimViTri->SetFocus();
	return r;

	CATCH: AppLog("Error=%s\n", GetErrorMessage(r));
	return r;
}
void TimViTriForm::HienThiCacViTriDangTimTrenMap(String *latLng) {
	int indexBegin = 0;
	int indexResult = 0;
	int i = 0;

	ArrayList * list = new ArrayList();
	list->Construct();

	String sMyLatLng = latLng->GetPointer();
	int lenghtChuoi = latLng->GetLength();
	if (lenghtChuoi > 0) {
		while (i < lenghtChuoi - 1) {
			sMyLatLng.IndexOf(',', i, indexResult);
			if (indexResult > -1) {
				String resultString;
				int len = indexResult - indexBegin;
				sMyLatLng.SubString(indexBegin, len, resultString);
				i = indexResult;
				indexBegin = indexResult + 1;
				list->Add(*(new String(resultString)));
			}
			i++;
		}
	}
	AppLog("Thong bao rang minh muon hien thi thong tin:%d" , list->GetCount());
	(static_cast<WebForm*> (pFormParent))->DatCacMakerTimKiemViTriLenMap(list);

}
